package com.tengju.bff.infrastructure.message.rabbitmq.consumer;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.rabbitmq.client.Channel;
import com.tengju.bff.application.wx.WxUserGradeService;
import com.tengju.bff.infrastructure.message.rabbitmq.consumer.dto.RoleGradeMessage;
import com.tengju.bff.infrastructure.shared.MqMessageConstants;
import com.tengju.bff.application.shared.SkipMessageException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Slf4j
@Component
public class AbmGradeConsumer {

    private final WxUserGradeService wxUserGradeService;

    public AbmGradeConsumer(WxUserGradeService wxUserGradeService) {
        this.wxUserGradeService = wxUserGradeService;
    }

    @RabbitHandler
    @RabbitListener(queues = MqMessageConstants.TJ_ROLE_LEVEL_QUEUE, containerFactory = "abmGradeContainerFactory")
    public void gradeConsumer(@Payload String message,
                              Channel channel,
                              @Headers Map<String, Object> headers) {
        log.info("abm升降级消息:{}", message);
        long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        if (StringUtils.isBlank(message)) {
            log.warn("abm升降级消息为空，跳过该消息");
            ack(channel, deliveryTag);
            return;
        }
        Gson gson = new GsonBuilder().serializeNulls().create();
        RoleGradeMessage roleGradeMessage = gson.fromJson(message, RoleGradeMessage.class);
        try {
            //升级为经销商的情况下进行企业微信成员创建
            if (roleGradeMessage.gradeDealer()) {
                wxUserGradeService.dealerWechatCreate(roleGradeMessage.getIdCode(), roleGradeMessage.getBizId());
            }
            ack(channel, deliveryTag);
        } catch (SkipMessageException e) {
            log.warn("跳过该abm升降级消息:{},exception:{}", gson.toJson(roleGradeMessage), e.getMessage(), e);
            ack(channel, deliveryTag);
        } catch (Throwable e) {
            log.error("该abm升降级消息消费异常:{},exception:{}", gson.toJson(roleGradeMessage), e.getMessage(), e);
            ack(channel, deliveryTag);
        }


    }

    private void ack(Channel channel, long tag) {
        try {
            channel.basicAck(tag, true);
        } catch (IOException e) {
            throw new RuntimeException("Message ack failed", e);
        }
    }


    private void nack(Channel channel, long tag) {
        try {
            channel.basicNack(tag, true, true);
        } catch (IOException e) {
            throw new RuntimeException("Message nack failed", e);
        }
    }


}
